package com.lfy.spring.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author leifengyang
 * @version 1.0
 * @date 2025/8/9 20:38
 * @description:
 */
@Component
@Aspect
public class UserAspect {


    @Before("execution(public void com.lfy.spring.service.UserService.printUser(..))")
    public void logStart(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String name = signature.getMethod().getName();
        System.out.println("logStart：正在执行："+name);
    }


    @AfterReturning("execution(public void com.lfy.spring.service.UserService.printUser(*,int))")
    public void logReturn(){
        System.out.println("logReturn");
    }


    @AfterThrowing("execution(public void com.lfy.spring.service.UserService.printUser(*,int))")
    public void logException(){
      System.out.println("logException");
    }

    @After("execution(* com.lfy.spring.service.UserService.*(..))")
    public void logEnd(){
       System.out.println("logEnd");
    }

    @Before("@annotation(com.lfy.spring.aspect.Hello)")
    public void hahah(){

    }

    @Around("execution(* com.lfy.spring.service.UserService.helloAbc(..))")
    public Object around(ProceedingJoinPoint joinPoint){

        Object result = null;

        try {
            //TODO 前置通知

            //放行目标方法执行
            result = joinPoint.proceed();

            //TODO 返回通知
        } catch (Throwable e) {
            //TODO 异常通知；推荐环绕通知中异常也要抛出
            throw new RuntimeException(e);
        } finally {
            //TODO 后置通知
        }

        return result;
    }


}
